package it.espsoft.easysuperenalotto.logic;

import it.espsoft.easysuperenalotto.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import android.widget.BaseAdapter;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

public class NumberGridAdapter extends BaseAdapter {  
	private static final int MAX_NUMBERS = 6;
	private static final int SELECTED_BTN_COLOR = Color.WHITE;
	private static final int UNSELECTED_BTN_COLOR = Color.DKGRAY;
    private Context mContext;  
    
    private String[] texts = { "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9","10",
				    		  "11", "12", "13", "14", "15", "16", "17", "18", "19","20",
				    		  "21", "22", "23", "24", "25", "26", "27", "28", "29","30",
				    		  "31", "32", "33", "34", "35", "36", "37", "38", "39","40",
				    		  "41", "42", "43", "44", "45", "46", "47", "48", "49","50",
				    		  "51", "52", "53", "54", "55", "56", "57", "58", "59","60",
				    		  "61", "62", "63", "64", "65", "66", "67", "68", "69","70",
				    		  "71", "72", "73", "74", "75", "76", "77", "78", "79","80",
				    		  "81", "82", "83", "84", "85", "86", "87", "88", "89","90"};
    
    List<Button> buttons = null;
    private TreeSet<Integer> selected = null;
    
    private int superstar = -1;
    
    public TreeSet<Integer> getSelected() {
		return selected;
	}

    public int getSuperstar(){
    	return superstar;
    }
    
    public NumberGridAdapter(Context c) {  
	    mContext = c;  
	    buttons = new ArrayList<Button>();
	    selected = new TreeSet<Integer>();
    }  
     
    public int getCount() {  
    	return texts.length;  
    }  
     
    public Object getItem(int position) {  
    	return null;  
    }  
     
    public long getItemId(int position) {  
    	return position;  
    }  
    
    public void mostraGiocata(Context c) {
		String msg = "";
		Iterator<Integer> it = selected.iterator();
		while(it.hasNext()){
			msg += it.next()+" ";
		}
		if(superstar != -1)
			msg += "Superstar : " + superstar;
		Toast.makeText(c, "La tua giocata : " + msg, Toast.LENGTH_LONG).show();
	}
    
    public void azzeraGiocata(){
    	int id = -1;
    	Iterator<Integer> it = selected.iterator();
		
    	while(it.hasNext()){
			id = it.next();
			buttons.get(id).setTextColor(UNSELECTED_BTN_COLOR);
			buttons.get(id).setBackgroundResource(R.drawable.num_griglia_normal);
		}
		
		if(superstar != -1){
			buttons.get(superstar).setTextColor(UNSELECTED_BTN_COLOR);
			buttons.get(superstar).setBackgroundResource(R.drawable.num_griglia_normal);
			superstar = -1;
		}
		
		selected.clear();
    }
    
    public boolean confermaGiocata(Context c){
    	if(selected.size() < MAX_NUMBERS){
    		Toast.makeText(c, "Devi selezionare " + MAX_NUMBERS + " numeri per continuare", Toast.LENGTH_LONG).show();
    		return false;
    	}
    	return true;
    }
    
    public void resetButtons(){
    	buttons.clear();
    }
    
    public void manageNumberSelection(Integer id){
    	if(selected.size() < MAX_NUMBERS) {
    		selected.add(id);
    	}
    }
    
    public void manageSuperstarSelection(Integer id){
    	superstar = id;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {  
     Button btn;  
     if (convertView == null) {    
      btn = new Button(mContext);
      btn.setLayoutParams(new GridView.LayoutParams(30, 30));
      btn.setOnClickListener(new OnClickListener() {
		  public void onClick(View v) {
			  Integer id = v.getId()+1;
			  if(superstar == id){
				  superstar = -1;
				  buttons.get(id).setTextColor(UNSELECTED_BTN_COLOR);
				  buttons.get(id).setBackgroundResource(R.drawable.num_griglia_normal);
			  }else if(selected.contains(id)){
				  selected.remove(id);
				  buttons.get(id).setTextColor(UNSELECTED_BTN_COLOR);
				  buttons.get(id).setBackgroundResource(R.drawable.num_griglia_normal);
			  }else if(selected.size() == MAX_NUMBERS - 1 && superstar == -1) {
				  Toast.makeText(mContext, "Puoi scegliere un numero SuperStar", Toast.LENGTH_LONG).show();
				  selected.add(id);
				  buttons.get(id).setTextColor(SELECTED_BTN_COLOR);
				  buttons.get(id).setBackgroundResource(R.drawable.num_griglia_pressed);
			  }else if( (selected.size() == MAX_NUMBERS) && (superstar == -1)) {
				  superstar = id;
				  buttons.get(id).setTextColor(SELECTED_BTN_COLOR);
				  buttons.get(id).setBackgroundResource(R.drawable.superstar_background);
			  }else if(selected.size() < MAX_NUMBERS) {
				  selected.add(id);
				  buttons.get(id).setTextColor(SELECTED_BTN_COLOR);
				  buttons.get(id).setBackgroundResource(R.drawable.num_griglia_pressed);
			  }
		  }
      });
     }else{  
    	 btn = (Button) convertView;  
     } 
     
     if(superstar == position+1){
    	btn.setBackgroundResource(R.drawable.superstar_background);
     	btn.setTextColor(SELECTED_BTN_COLOR);
     }else if(selected.contains(position+1)){
    	 btn.setBackgroundResource(R.drawable.num_griglia_pressed);
    	 btn.setTextColor(SELECTED_BTN_COLOR);
	 }else{
		 btn.setTextColor(UNSELECTED_BTN_COLOR);
		 btn.setBackgroundResource(R.drawable.num_griglia_normal);
	 }
	 
	 btn.setPadding(0, 0, 0, 0);
     btn.setText(texts[position]);   
     btn.setWidth(15);
     btn.setHeight(15);
     btn.setId(position);  
     buttons.add(btn);
     
     return btn;  
    }
	 
   }  
